(function() {
  app.controller('vipTradeCtrl', function(alertTip, centerdilog, tradeApi, $interval, quoteApi, header_m, $stateParams, $scope, local_m, vipIndex, $state, bottomdilog) {
    header_m
      .set({
        title_c: $stateParams.type === 'buy' ? '买入' : '卖出',
      });

    $scope.trade_info = {
      funcid: '10',
      price: null,
      ordercount: null,
      ordertype: null,
      bstype: $stateParams.type === 'buy' ? 'B' : 'S',
      ordertype_txt: '申报方式',
      stkcode: $stateParams.stkcode,
      ordercount_enable: '委托数量'
    };

    $scope.reset = function() {
      $scope.trade_info.price = null;
      $scope.trade_info.market = null;
      $scope.trade_info.ordercount = null;
      $scope.trade_info.ordertype = null;
      $scope.trade_info.ordertype_txt = '申报方式';
      $scope.trade_info.ordercount_enable = '委托数量';
    };

    $scope.init = function() {
      $scope.query_price();
      $scope.hold_info();
    };

    $scope.query_price = function(type) {
      if (!$scope.trade_info.stkcode || $scope.trade_info.stkcode.length !== 6) {
        $scope.reset();
        return;
      }

      tradeApi
        .etQuery({
          funcid: '8',
          stkcode: $scope.trade_info.stkcode,
          bstype: $scope.trade_info.bstype
        })
        .then(function(res) {
          if (type !== 'reload') {
            $scope.reset();
            $scope.trade_info.price = res[0][$scope.trade_info.bstype === 'B' ? 's1p' : 'b1p'];
            $scope.trade_info.ordercount_enable = res[0].max_qty;
            $scope.trade_info.market = res[0].market;
            $scope.trade_info.ordertype = '0';
            $scope.trade_info.ordertype_txt = '限价';
          }
          $scope.order_detail = res[0];
        });
    };

    $scope.trade_query = function() {
      tradeApi
        .etQuery({
          funcid: '10',
          market: $scope.trade_info.market,
          ordercount: $scope.trade_info.ordercount,
          stkcode: $scope.trade_info.stkcode,
          price: $scope.trade_info.price,
          ordertype: $scope.trade_info.ordertype,
          bstype: $scope.trade_info.bstype
        })
        .then(function(res) {
          alertTip.open({
            state: 1,
            msg: $stateParams.type === 'buy' ? '买入委托提交成功！' : '卖出委托提交成功！',
          });
        })
        .finally(function() {
          $scope.reset();
          $scope.trade_info.stkcode = null;
          $scope.order_detail = null;
        });
    };

    $scope.reload = function() {
      $scope.query_price('reload');
    };

    var timer = $interval(function() {
      $scope.reload();
    }, 3000);

    $scope.$on('$destroy', function() {
      $interval.cancel(timer);
      timer = null;
    });

    $scope.select_ways = function() {
      var list = null;
      if (!$scope.trade_info.market) return;
      if ($scope.trade_info.market === '1') {
        list = [
          { txt: '限价', value: '0' },
          { txt: '五档即成剩撤', value: '5' },
          { txt: '五档即成剩转限', value: '2' },
        ];
      } else if ($scope.trade_info.market === '2') {
        list = [
          { txt: '限价', value: '0' },
          { txt: '对方最优', value: '1' },
          { txt: '本方最优', value: '3' },
          { txt: '即成剩撤', value: '4' },
          { txt: '五档即成剩撤', value: '5' },
          { txt: '全成或撤销', value: '6' },
        ];
      } else {
        $scope.trade_info.ordertype = '0';
        $scope.trade_info.ordertype_txt = '限价';
        return;
      }
      bottomdilog
        .open({
          type: '2',
          list: list,
          list_click: function(it) {
            $scope.trade_info.ordertype = it.value;
            $scope.trade_info.ordertype_txt = it.txt;
          }
        });
    };

    $scope.code_input = function() {
      $scope.order_detail = null;
      $scope.query_price();
    };

    $scope.hold_info = function() {
      tradeApi
        .etQuery({
          funcid: '4',
        })
        .then(function(res) {
          angular.forEach(res, function(item) {
            if (item.income > 0) {
              item.rows_list_item_color = 'red';
            } else if (item.income < 0) {
              item.rows_list_item_color = '#30ae0e';
            } else {
              item.rows_list_item_color = 'black';
            }
          });
          $scope.rowList.data = res;
        });
    };

    $scope.bg_set = function(name) {
      return {
        background: 'url(img/' + name + '.png)no-repeat',
        backgroundSize: 'contain',
        backgroundPosition: 'center center',
      };
    };

    $scope.tade_confirm = function() {
      if (!$scope.trade_info.stkcode || $scope.trade_info.stkcode.length !== 6 || !$scope.trade_info.price || !$scope.trade_info.ordercount || !$scope.trade_info.ordertype) return;
      var tpl = '<p style="text-align:left;padding-left:15%">股票名称：<span>' + $scope.order_detail.stkname + '</span></p>' +
        '<p style="text-align:left;padding-left:15%;">股票代码：<span>' + $scope.trade_info.stkcode + '</span></p>' +
        '<p style="text-align:left;padding-left:15%">委托方向：<span>' + ($scope.trade_info.bstype === 'B' ? '买入' : '卖出') + '</span></p>' +
        '<p style="text-align:left;padding-left:15%">申报方式：<span>' + $scope.trade_info.ordertype_txt + '</span></p>' +
        '<p style="text-align:left;padding-left:15%">委托价格：<span>' + $scope.trade_info.price + '</span></p>' +
        '<p style="text-align:left;padding-left:15%">委托数量：<span>' + $scope.trade_info.ordercount + '</span></p>';
      centerdilog
        .open({
          ok: function() {
            $scope.trade_query();
          },
          tpl: '<div>' + tpl + '</div>',
        });
    };

    $scope.rowList = {
      data: null,
      title: ['证券/市值', '参考盈亏', '持仓/可用', '成本价/现价'],
      color_key: 'rows_list_item_color',
      row1: [
        { key: 'stkname' },
        { key: 'income' },
        { key: 'amt' },
        { key: 'price' },
      ],
      row2: [
        { key: 'marketval' },
        { key: 'incomeratio' },
        { key: 'enable' },
        { key: 'lastprice' },
      ],
      click: function(obj) {
        $scope.reset();
        $scope.trade_info.stkcode = obj.stkcode;
        $scope.code_input();
      },
    }


    $scope.operation = function(operation_type, speed, key) { // 用于加减运算
      if (($scope.trade_info[key] == 0 || $scope.trade_info[key] < 0) && operation_type === 'minus') return;
      if (operation_type === 'add') {
        $scope.trade_info[key] = Math.round($scope.trade_info[key] * 100 + speed * 100) / 100;
      }
      if (operation_type === 'minus') {
        if (!$scope.trade_info[key]) return;
        $scope.trade_info[key] = Math.round($scope.trade_info[key] * 100 - speed * 100) / 100;
      }
    };

  });

})();